var websocket = null;
$(function () {
    if (window['WebSocket']) {
        websocket = new WebSocket("ws://" + window.location.host + CONTEXT_PATH + '/websocket');
    } else {
        websocket = new SockJS(CONTEXT_PATH + '/websocket/socketjs');
    }

    websocket.onopen = function (event) {
        console.log('open', event);
    };

    websocket.onmessage = function(event, isOnLoad) {
        var noticeArray = null;
        if(isOnLoad) {
            noticeArray = event;
        } else {
            noticeArray = JSON.parse(event.data);
        }
        if(noticeArray) {
            var noticeData = noticeArray.data; //将字符串转化为json对象
            //由于公告的是json数组，而任务超时预警等是json对象，因此要分开不同处理
            if (noticeArray.type == "1" && noticeData instanceof Array) {     //公告的处理\
                if (noticeData && noticeData.length > 0) {//有公告就处理
                    $("#noticeIcon .noticeNum").text(noticeData.length);//每次设置消息的数量
                    noticeList = null;  //先清空全局变量在讲最新的通知列表信息赋给该变量
                    noticeList = noticeData;
                    $("#noticeUl").empty();//清空ul里的内容保证是最新的消息
                    for (var j = 0; j < noticeList.length; j++) {
                        var noticeliHtml = "<li><a class='noticeClickA " + noticeList[j].id + "-" + noticeList[j].idPerson + "'" + " href='javascript:;'>"
                            +"<div class='inline-block'>"
                            +"<span class='topbar-notice-link'><img alt='' style='border: 0; margin-right: 10px' src='" +CONTEXT_PATH+ "/static/images/new.gif'>"+ noticeList[j].noticeTitle + "</span>"
                            +"<span class='topbar-notice-time'>" + noticeList[j].createdTime + "</span>"
                            +"</div>"
                            +"<span class='inline-block topbar-notice-class' >" + "<span class='topbar-notice-class-name'>"
                        if (noticeList[j].noticeType == "SystemNotice") {
                            noticeliHtml = noticeliHtml + "公告";
                        }
                        noticeliHtml += "</span></span></a></li>";
                        $("#noticeUl").append(noticeliHtml);
                    }
                    for (var i = 0; i < 1; i++) {  //公告都先弹出最新的前1条，其他的点击更多后再未处理的在图标的通知中显示并给予处理
                        //公告
                        if (noticeData[i].noticeType == "SystemNotice") {
                            operatorSystemNotice(noticeData[i].id, noticeData[i].idPerson, $("#noticeUl").find("li").eq(0));
                        }
                    }
                }
            }   //任务超时预警
            if ((noticeArray.type == "2" || noticeArray.type == "5") && noticeData.noticeType == "TaskOverTime") {
                $.messager.show({
                    title: '任务超时预警',
                    msg: noticeData.noticeContent,
                    timeout:5000,
                    showType: 'show'
                });
            }
            //任务刷新
            if (noticeArray.type == "3" && noticeArray.data) {
                try {
                    var taskRecord =  noticeArray.data;
                    var approveTaskUrl = CONTEXT_PATH+'/process/index?contractId=' + taskRecord.idContract
                        + '&productCode=' + taskRecord.productCode
                        + '&productId=' + taskRecord.productId
                        + '&processVersion=' + taskRecord.version
                        + '&processInstanceId=' + taskRecord.processId
                        + '&processCode=' + taskRecord.processCode
                        + '&nodeId=' + taskRecord.nodeId
                        + '&nodeName=' + encodeURIComponent(taskRecord.curPhase)
                        + '&taskId=' + taskRecord.idCurTask
                        + '&customerId=' + taskRecord.customerId
                        + '&customerName=' + taskRecord.customerName;
                    $('#_right').attr('src', encodeURI(approveTaskUrl));
                } catch (e) {
                    console.log(e);
                }
            }
            //销售取消合同订单(从合同管理系统的MQ推过来的销售取消通知),暂定将消息提醒框显示在顶部中间的位置
            if (noticeArray.type == "4") {
                $.messager.alert("销售取消合同订单", "销售已经取消该合同！" + noticeArray.data, "error");
                $('#_right').attr('src', CONTEXT_PATH + '/myDoTasks');
            }

            //销售经理否决取消合同订单(从合同管理系统的MQ推过来的销售经理否决通知),暂定将消息提醒框显示在顶部中间的位置
            if (noticeArray.type == "7") {
                $.messager.alert("销售经理否决","否决类型：销售经理否决！"+noticeArray.data,"error");
                $('#_right').attr('src', CONTEXT_PATH +'/myDoTasks');
            }

        }
    };

});